<?php
	// On prolonge la session
	session_start();
	$id_anim='';
	$code='';

	require_once('../../classes/database.php');
	$idconnect=Database::getInstance();


		// On teste si la variable de session existe et contient une valeur
		if(empty($_SESSION['id_anim'])){
			// Si inexistante ou nulle, on redirige vers la page de saisie du code
			header('Location: ../recherche.php');
			exit();
		}
		else{
			$id_anim=$_SESSION['id_anim'];
			$code=$_SESSION['code'];
		}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="../../style.css" />
        <?php
			require_once(dirname(__FILE__)."/onglet_animal.php");
			$onglet = affiche_onglets($_SESSION['membre_id']);
		?>
		<!--[if lt IE 9]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
        <!--[if lte IE 7]>
        <link rel="stylesheet" href="../../style_ie.css" />
        <![endif]-->
        <title>Base de données microcèbes</title>
    </head>

    <body>
		<?php include((dirname(dirname(dirname(__FILE__))))."/menu.php"); ?>
		<?php include((dirname(dirname(__FILE__)))."/calcul_age.php"); ?>

		<section id="main">
			<header>
				<h1>Recherche - Reproduction animal <?php echo $code;?></h1>
			</header>
			<?php include((dirname(dirname(__FILE__)))."/menu_recherche.php"); ?>
			<section id="animal">
				<?php
					echo $onglet;
				?>

				<section id="contenu">
					<h1>Informations sur les parents:</h1>
					<p>
						<?php
							$query = pg_query($idconnect,"SELECT id_portee FROM animal WHERE id_animal='".$id_anim."'");
							$back = pg_fetch_assoc($query);

							$int = pg_num_rows($query);
							if($int==0 or $back["id_portee"]==NULL){echo("<p><label>Pas d'informations disponibles</label></p>");}
							else{
								if($back["id_portee"]){
									//récupération de la mère
									$query2 = pg_query($idconnect,"SELECT id_animal_mere FROM Portee WHERE id_portee='".$back["id_portee"]."'");
									while ($back2 = pg_fetch_assoc($query2)){
										if($back2["id_animal_mere"]){
											$query3 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM animal WHERE id_animal='".$back2["id_animal_mere"]."'");
											$back3 = pg_fetch_assoc($query3);
											echo "<label><strong>Code Mère: </strong>";
											if($back3["code_animal"]){echo $back3["code_animal"]."</label><br/>";}
											elseif($back3["code_autre_animalerie"]){echo $back3["code_autre_animalerie"]."</label><br/>";}
										}
									}
									//récupération des pères
									$query4 = pg_query("SELECT * FROM Est_le_pere WHERE id_portee='".$back["id_portee"]."'");
									$i=1;
									while ($back4 = pg_fetch_assoc($query4)){
										if($back4["id_animal"]){
											$query5 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM animal WHERE id_animal='".$back4["id_animal"]."'");
											$back5 = pg_fetch_assoc($query5);
											echo "<label><strong>Code père ".$i.": </strong>";
											if($back5["code_animal"]){echo $back5["code_animal"];}
											elseif($back5["code_autre_animalerie"]){echo $back5["code_autre_animalerie"];}
										}
										if($back4["type_pere"]){
											echo " ".$back4["type_pere"];
										}
										echo "</label><br/>";
										$i++;
									}
									//infos sur les petits
									$j=1;
									$query5 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM Animal WHERE id_portee='".$back["id_portee"]."' AND id_animal!=".$id_anim);
									while ($back5 = pg_fetch_assoc($query5)){
										echo "<label><strong>Code autre bébé ".$j.": </strong>";
										if($back5["code_animal"]){echo $back5["code_animal"]."</label><br/>";}
										elseif($back5["code_autre_animalerie"]){echo $back5["code_autre_animalerie"]."</label><br/>";}
										$j++;
									}
								}
							}
						?>
					</p>

					<h1>Informations sur la descendance:</h1>

					<?php
						$query2 = pg_query($idconnect,"SELECT sexe FROM animal WHERE id_animal='".$id_anim."'");
						$back2 = pg_fetch_assoc($query2);
						$int2 = pg_num_rows($query2);
						if($int2==0 or $back2["sexe"]==NULL){echo("<p><label>Pas de données</label></p>");}
						else{
							if($back2["sexe"]=="F"){ //si l'animal courant est une femelle
								$query = pg_query($idconnect,"SELECT * FROM portee WHERE id_animal_mere='".$id_anim."'");
								$int3 = pg_num_rows($query);
								if($int3==0){echo("<p><label>Pas d'informations disponibles</label></p>");}
								else{
									$k=1;
									while ($back = pg_fetch_assoc($query)){
										echo "<p>";
										echo "<label class=\"titre2\"><strong>Portée $k:</strong></label><br/>";
										//infos de la portee
										if($back["date_fecondation"]){ echo "<label><strong>Date de fécondation: </strong>".date("d/m/Y", strtotime($back["date_fecondation"]))."</label><br/>";}
										if($back["a_terme"]=="t"){echo "<label><strong>A terme: </strong> Oui</label><br/>";}
										elseif($back["a_terme"]=="f"){echo "<label><strong>A terme: </strong> Non</label><br/>";}
										if($back["nbr_petits"]){echo "<label><strong>Nombre total de petits: </strong>".$back["nbr_petits"]."</label><br/>";}
										if($back["nbr_petits_survecu"]){echo "<label><strong>Nombre de petits ayant survecu: </strong>".$back["nbr_petits_survecu"]."</label><br/>";}
										echo "<br/>";

										if($back["id_portee"]){
											//infos sur les males
											$query3 = pg_query($idconnect,"SELECT * FROM Est_le_pere WHERE id_portee='".$back["id_portee"]."'");
											$i=1;
											while ($back3 = pg_fetch_assoc($query3)){
												if($back3["id_animal"]){
													$query4 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM animal WHERE id_animal='".$back3["id_animal"]."'");
													$back4 = pg_fetch_assoc($query4);
													echo "<label><strong>Code mâle ".$i.": </strong>";
													if($back4["code_animal"]){echo $back4["code_animal"];}
													elseif($back4["code_autre_animalerie"]){echo $back4["code_autre_animalerie"];}
												}
												if($back3["type_pere"]){
													echo " ".$back3["type_pere"];
												}
												echo "</label><br/>";
												$i++;
											}
											echo "<br/>";
											//infos sur les petits
											$j=1;
											$query5 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM Animal WHERE id_portee='".$back["id_portee"]."'");
											while ($back5 = pg_fetch_assoc($query5)){
												echo "<label><strong>Code bébé ".$j.": </strong>";
												if($back5["code_animal"]){echo $back5["code_animal"]."</label><br/>";}
												elseif($back5["code_autre_animalerie"]){echo $back5["code_autre_animalerie"]."</label><br/>";}
												$j++;
											}
										}
										echo "<br/></p>";
										$k++;
									}
								}
							}
							elseif($back2["sexe"]=="M"){ //si l'animal courant est un mâle
								$query = pg_query($idconnect,"SELECT * FROM est_le_pere WHERE id_animal='".$id_anim."'");
								$int3 = pg_num_rows($query);
								if($int3==0){echo("<p><label>Pas d'informations disponibles</label></p>");}
								else{
									while ($back = pg_fetch_assoc($query)){
										echo "<p>";
										if($back["id_portee"]){
											$query2 = pg_query($idconnect,"SELECT * FROM portee WHERE id_portee='".$back["id_portee"]."'");
											$k = 1;
											while ($back2 = pg_fetch_assoc($query2)){
												//infos sur la portee
												echo "<label class=\"titre2\"><strong>Portée $k:</strong></label><br/>";
												if($back2["date_fecondation"]){ echo "<label><strong>Date de fécondation: </strong>".date("d/m/Y", strtotime($back2["date_fecondation"]))."<br/>";}
												if($back2["a_terme"]=="t"){echo "<label><strong>A terme: </strong> Oui</label><br/>";}
												elseif($back2["a_terme"]=="f"){echo "<label><strong>A terme: </strong> Non</label><br/>";}
												if($back2["nbr_petits"]){echo "<label><strong>Nombre total de petits: </strong>".$back2["nbr_petits"]."</label><br/>";}
												if($back2["nbr_petits_survecu"]){echo "<label><strong>Nombre de petits ayant survecu: </strong>".$back2["nbr_petits_survecu"]."</label><br/>";}
												echo "<br/>";

												//informations sur la femelle
												if($back2["id_animal_mere"]){
													$query1 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM animal WHERE id_animal='".$back2["id_animal_mere"]."'");
													$back1 = pg_fetch_assoc($query1);
													echo "<label><strong>Code femelle: </strong>";
													if($back1["code_animal"]){echo $back1["code_animal"];}
													elseif($back1["code_autre_animalerie"]){echo $back1["code_autre_animalerie"];}
													echo "</label><br/";
												}
												echo "<br/>";

												//infos sur les autres males
												$query3 = pg_query($idconnect,"SELECT * FROM Est_le_pere WHERE id_portee='".$back["id_portee"]."' AND id_animal!='".$id_anim."'");
												$i=2;
												while ($back3 = pg_fetch_assoc($query3)){
													if($back3["id_animal"]){
														$query4 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM animal WHERE id_animal='".$back3["id_animal"]."'");
														$back4 = pg_fetch_assoc($query4);
														echo "<label><strong>Code mâle ".$i.": </strong>";
														if($back4["code_animal"]){echo $back4["code_animal"];}
														elseif($back4["code_autre_animalerie"]){echo $back4["code_autre_animalerie"];}
													}
													if($back3["type_pere"]){
														echo " ".$back3["type_pere"];
													}
													echo "</label><br/>";
													$i++;
												}
												echo "<br/>";

												//infos sur les petits
												$j=1;
												$query5 = pg_query($idconnect,"SELECT code_animal,code_autre_animalerie FROM Animal WHERE id_portee='".$back["id_portee"]."'");
												while ($back5 = pg_fetch_assoc($query5)){
													echo "<label><strong>Code bébé ".$j.": </strong>";
													if($back5["code_animal"]){echo $back5["code_animal"]."</label><br/>";}
													elseif($back5["code_autre_animalerie"]){echo $back5["code_autre_animalerie"]."</label><br/>";}
													$j++;
												}
												$k++;
											}
										}
										echo "<br/></p>";
									}
								}
							}
						}
					?>
				</section>
			</section>

		</section>
	</body>
</html>
